package cn.cas.mango.util;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class StringUtil {



  public static Integer getOccurrence(String str, String findStr) {
    int count = 0;
    int index = 0;


    while ((index = str.indexOf(findStr, index)) != -1) {
      count++;
      index += findStr.length(); // 移动到子字符串之后，避免重复计数
    }

    return count;
  }

  public static boolean judgeValidKeywords(String string) {
    for (char a: string.toCharArray()) {
      if ("./\\%; |>-*()!@#\":=^~➡".indexOf(a) != -1)
        return false;
    }
    return true;
  }

  public static String getCutName(String name, int maxLength) {
    if (name == null) return "...";
    if (name.length() > maxLength) {
      name = name.substring(0, maxLength) + "...";
    }
    return name;
  }

  public static boolean isChineseString(String input) {
    int chineseCharCount = 0;
    int validCharCount = 0; // 用于计数非标点的字符
    int length = Math.min(input.length(), 100); // 检查最多前100个字符

    for (int i = 0; i < length; i++) {
      char ch = input.charAt(i);
      if (!isPunctuation(ch)) { // 忽略标点符号
        validCharCount++;
        if (isChinese(ch)) {
          chineseCharCount++;
        }
      }
    }

    // 如果没有有效字符，直接返回false
    if (validCharCount == 0) {
      return false;
    }

    // 返回true如果超过一半的非标点字符是中文
    return chineseCharCount > (validCharCount / 2);
  }

  private static boolean isChinese(char ch) {
    // 基于Unicode块进行中文字符检查
    return (ch >= 0x4E00) && (ch <= 0x9FFF);
  }

  public static boolean isPunctuation(char ch) {
    // 检查字符是否为中文标点符号
    if ((ch >= 0x3000) && (ch <= 0x303F)) {
      return true;
    }
    // 检查字符是否为常见的英文标点符号
    return (ch == ',' || ch == '.' || ch == '?' || ch == '!' || ch == ':' || ch == ';' ||
        ch == '\'' || ch == '\"' || ch == '-' || ch == '(' || ch == ')' ||
        ch == '[' || ch == ']' || ch == '{' || ch == '}' || ch == '@' ||
        ch == '#' || ch == '$' || ch == '%' || ch == '^' || ch == '&' ||
        ch == '*' || ch == '+' || ch == '=' || ch == '<' || ch == '>' ||
        ch == '/' || ch == '\\' || ch == ' ');
  }
}
